const util = require('../../utils/util.js');
const user = require('../../utils/user.js');

const app = getApp();
Page({
  data: {
    radioOne:'',
    radioChange:false,
    checkState:false,
    result:'',
    // userId:''
  },
  onChangeGroup (event) {
    if(this.data.result.length>0){
      this.setData({
        result:[]
      })
    }else{
      this.setData({
        result:['agree']
      })
    }
  },
  switchTreat(){
    wx.navigateTo({
      url:'/pages/Composition/Composition'
    });
  },
  userService(){
    wx.navigateTo({
      url:'/pages/Agreement/Agreement'
    });
  },
  onShareAppMessage() {
    return {};
  },
  getUserProfile() {
    const  _this = this;
    if(this.data.result.length==0){
      wx.showModal({
        title: '同意协议即可登录',
        content:'已阅读并同意用户服务协议、隐私协议',
        showCancel: true,//是否显示取消按钮
        cancelText:"不同意",//默认是“取消”
        cancelColor:'rgb(167, 167, 167)',//取消文字的颜色
        confirmText:"同意",//默认是“确定”,同意并完善个人信息
        confirmColor: 'rgb(31, 103, 220)',//确定文字的颜色
        success: function (res) {
          _this.onChangeGroup()
          if (res.confirm) {
            wx.getSetting({
              success(res) {
                // console.log(res,'getsetting11111')
        wx.getUserProfile({
              desc: '使用户得到更好的体验',
              success: (res) => {
                user.loginByWeixin(res.userInfo).then(res => {
                  if(res.errno===200){
                    let UserId =res.data.userInfo.id
                    if(res.data.userInfo.phone==="" || res.data.userInfo.phone ===null){
                      wx.navigateTo({url: '/pages/MyInfo/MyInfo'});
                    }else{
                       wx.navigateBack({
                          delta: 1
                        })
                    }
                    wx.setStorageSync("userId",UserId)
                    app.globalData.hasLogin = true;
                  }
                }).catch((err) => {
                  app.globalData.hasLogin = false;
                  util.showErrorToast('微信登录失败');
                });
              },
              fail: res => {
                return  util.showErrorToast('获取用户信息失败');
              }
            })
              }
            })
          }else{
            _this.setData({
              result:[]
            })
          }
        },
      })
    }else{
wx.getSetting({
        success(res) {
  wx.getUserProfile({
        desc: '使用户得到更好的体验',
        success: (res) => {
          user.loginByWeixin(res.userInfo).then(res => {
            if(res.errno===200){
              let UserId =res.data.userInfo.id
              if(res.data.userInfo.phone==="" || res.data.userInfo.phone ===null){
                wx.navigateTo({url: '/pages/MyInfo/MyInfo'});
              }else{
                 wx.navigateBack({
                    delta: 1
                  })
              }
              wx.setStorageSync("userId",UserId)
              // app.globalData.id =UserId;
              app.globalData.hasLogin = true;
            }
          }).catch((err) => {
            app.globalData.hasLogin = false;
            util.showErrorToast('微信登录失败');
          });
        },
        fail: res => {
          return  util.showErrorToast('获取用户信息失败');
        }
      })
        }
      })
    }
  },
});
